<template>
	<view id="user-page">
		<view class="page-header">
			<view class="user-info">
				<image class="header-img"  v-if="userInfo.headimgurl" :src="userInfo.headimgurl" mode=""></image>
				<image class="header-img" v-else src="../../static/img/wx_login.png" mode=""></image>
				<view class="phone-name">
					<text class="name">{{userInfo.true_name}}</text>
					<text class="phone">{{userInfo.phone}}/{{userInfo.code}}</text>
				</view>
			</view>
			<!-- <view class="task-num-box">-->
			<!--	<view class="task-type-box">-->
			<!--		<view class="number">{{ userInfo.sale_complete_num == null ? '0' : userInfo.sale_complete_num }}</view>-->
			<!--		<view class="title">销量总数</view>-->
			<!--	</view>-->
			<!--	<view class="task-type-box">-->
			<!--		<view class="number">{{ userInfo.flow_complete_num == null ? '0' : userInfo.flow_complete_num }}</view>-->
			<!--		<view class="title">浏览总数</view>-->
			<!--	</view>-->
			<!--	<view class="task-type-box">-->
			<!--		<view class="number">{{ userInfo.assess_complete_num == null ? '0' : userInfo.assess_complete_num }}</view>-->
			<!--		<view class="title">评价总数</view>-->
			<!--	</view>-->
			<!--</view>-->
			<view class="task-num-box" @click="toMoneyPage(1)">
                <view class="task-type-box" >
                  <view class="number">{{moneyData.capital}}</view>
                  <view class="title">我的积分</view>
                </view>
				<view class="task-type-box">
					<view class="number">{{moneyData.commission}}</view>
					<view class="title">奖励积分</view>
				</view>
			</view>
		</view>
		<view class="row-list">
      <view class="row-cell" @click="toCashPage">
        <view class="row-cell-icon el-icon-ali-invitation-code bank-color"></view>
        <view class="row-cell-title">兑换商品（提现）</view>
      </view>
      <view class="row-cell" @click="toWithdrawalPage">
        <view class="row-cell-icon el-icon-ali-bank bank-color"></view>
        <view class="row-cell-title" style="color: red;font-weight: bold">兑换明细（对账专用）</view>
      </view>
			<view class="row-cell" @click="toAccountPage">
				<view class="row-cell-icon el-icon-ali-account account-color"></view>
				<view class="row-cell-title">我的账号</view>
				<!-- <view class="row-cell-right">
					<view class="right-content">{{userInfo.platform_name}}</view>
				</view> -->
			</view>
			<view class="row-cell" @click="toBindInfoPage">
				<view class="row-cell-icon el-icon-ali-info info-color"></view>
				<view class="row-cell-title">信息绑定</view>
			</view>
			<view class="row-cell" @click="acceptDisciples">
				<view class="row-cell-icon el-icon-ali-disciple disciple-color"></view>
				<view class="row-cell-title">{{userInfo.identity_status == 1 ? '收徒挣钱':'晋升师傅'}}</view>
			</view>
			<view class="row-cell" @click="toServicePage">
				<view class="row-cell-icon el-icon-ali-service service-color"></view>
				<view class="row-cell-title">联系客服</view>
			</view>
			<view class="row-cell" @click="toSetPage">
				<view class="row-cell-icon el-icon-ali-set set-color"></view>
				<view class="row-cell-title">设置</view>
			</view>

		</view>
		<!--<view class="row-list-ad">
			<image src="../../static/img/red_ad.gif" @tap="jumpToHomgbei"  mode="widthFix" style="width: 90%"></image>
		</view>-->

		<u-modal
			:showModal="showModal"
			title="晋升师傅"
			@cancleModal = "showModal = false"
			@confirmModal="promotionMaster"
			>
			<view>
				<view>请向师傅购买升级码，使用后将进入快速审核通道</view>
				<view>
					<input class="upgrade-code-input" type="text" v-model="upgradeCode" placeholder="请输入升级码"/>
				</view>
			</view>
		</u-modal>
		<u-modal
			:showModal="masterModal"
			title="晋升师傅"
			content="是否晋升师傅？"
			@cancleModal = "masterModal = false"
			@confirmModal="promotionMaster"
			>
		</u-modal>
		<u-modal :showModal="showNotice" title="公告" :showCancel="false" @confirmModal="showNotice=false">
			<view id="notice-detail">
				<view class="titie">
					{{detailData.title}}
				</view>
				<view class="time">
					{{detailData.create_time}}
				</view>
				<view class="content">
					{{detailData.content}}
				</view>
			</view>
		</u-modal>
		<u-modal
				:showModal="$store.getters.urgeDialogFlag && $store.getters.urgeDialogNum != 0"
				title="催收消息"
				:autoClose="false"
				:showCancel="false"
				confirmColor="#000"
				confrimTitle="查看消息"
				@confirmModal="toMessageDetail">
			<view id="urge-detail">
				你有<text>{{" " + $store.getters.urgeDialogNum + " "}}</text>个订单<text>{{" "}}未确认收货</text>
			</view>
		</u-modal>
	</view>
</template>

<script>
import {
  apiNoticeDetail,
  apiPushUser,
  apiPromotionPushUser,
  apiApplyMaster,
  apiPushIndex,
  apiCommissionCapitalCash,
  apiWeChatOfficialAccount,
  apiWechatLogin, pushAllWithdraw, allCommissionCapitalCash
} from '../../../http/apis.js'
	import uModal from '../../components/uni-modal/uni-modal.vue'
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			...mapState(['token'])
		},
		components:{
			uModal
		},
		data(){
			return {
				detailData:{
					title:'',
					create_time:'',
					content:'',
				},
				userInfo: {},
				showNotice:false,
				showModal: false,
				isShowOpenId: '',
				openId: '',
				clickNum: 0,
				lastTime: '',
				upgradeCode: '',
				masterModal: false,
				moneyData: {}
			}
		},
		onShow() {
			this.getUserInfo()
			this.getPushIndex()
		},
		onLoad() {

		},
		methods:{
			// 去消息列表页面
			toMessageDetail() {
				uni.switchTab({
					url: "../notice/noticeList"
				})
			},
			jumpToHomgbei(){
				uni.navigateTo({
					url: '/pages/webview/index?url=https%3A%2F%2Fxim5dhgrap.kuaizhan.com%2F%3FTk2Rb0'
				})
			},
			getNoticeDetail(id){
				apiNoticeDetail({
					token: this.token,
					noticeId: id
				}).then(res =>{
					console.log(res)
				    if(res.data.code == 1)  {
						this.detailData = res.data.msg
						this.showNotice=true
					}
				}).catch(res => {
					this.$msg.showToast(res.data.msg)
				})
			},
			getPushIndex() {
				apiPushIndex({
					token: this.token
				}).then(res => {
					if (res.data.code == 1) {
						this.moneyData = res.data.msg;
						// if(this.moneyData.notice_list && this.moneyData.notice_list.length>0){
						// 		this.getNoticeDetail(this.moneyData.notice_list[0].id)
						// }
					}
				})
				.catch(res => {
					this.$msg.showToast(res.data.msg);
				});
			},
			toMoneyPage(type){
				apiCommissionCapitalCash({
					token: this.token,
					type: type
				}).then(res => {
					if(res.data.code == 1){
						this.getWeChatOfficialAccount(type)
					} else if (res.data.code == 2012 || res.data.code == 2013) {
					    this.$msg.showToast('请先绑定个人信息');
						setTimeout(() => {
							uni.navigateTo({
								url: '../bindInfo/bindInfo'
							});
						}, 1000);
					} else {
						this.$msg.showToast(res.data.msg);
					}
				})
				.catch(res => {
					this.$msg.showToast(res.data.msg);
				});
			},
			getWeChatOfficialAccount(type){
				apiWeChatOfficialAccount({
				    token: this.token
				}).then(res =>{
				    if(res.data.code == 1)  {
						if(res.data.allNumStatus == 2){
							//是否授权
							if (res.data.msg.authorize == 2){
								if (this.isWechat()) {
									let appid = res.data.msg.appid
                  // 没有前置机时
									// let link = this.GLOBAL.MAIN_URL + 'pages/homeWxAuth/homeWxAuth?appId=' + appid
                  // 有前置机时
                  let link = this.GLOBAL.WX_CALLBACK + 'baseapi/push/bindWeChat?appId=' + appid
									let uri = encodeURIComponent(link);
									let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
									window.location.href = authURL;
								} else {
									this.$msg.showToast('请到微信上打开该平台')
								}
							} else {
								if(res.data.msg.attention == 2) {
									this.$msg.showToast('请先关注公众号')
									setTimeout(() => {
										uni.navigateTo({
											url: '../attentionPublic/attentionPublic'
										});
									}, 1000);
								}
							}

						} else if(res.data.allNumStatus == 1){
							uni.navigateTo({
								url: '../money/fundsDetail?type=' + type
							});
						}
					} else {
						this.$msg.showToast(res.data.msg)
					}
				}).catch(res => {
					this.$msg.showToast(res.data.msg)
				})
			},
			getUserInfo(){
				apiPushUser({
					token: this.token
				}).then(res =>{
				    if(res.data.code == 1)  {
						this.userInfo = res.data.msg
					}
				}).catch(res => {
					this.$msg.showToast(res.data.msg)
				})
			},
			acceptDisciples(){
				// identity_status = 2 是徒弟
				if(this.userInfo.identity_status == 2){
					apiPromotionPushUser({
						token: this.token
					}).then(res =>{
					    if(res.data.code == 1)  {
							if (res.data.identity_status == 1) {
								this.getUserInfo()
							}
						} else if (res.data.code == 2002) {
							// this.showModal = true
							// this.upgradeCode = ''
							this.masterModal = true
						}
						else if (res.data.code == 2012 || res.data.code == 2013){
							this.$msg.showToast('请先绑定个人信息')
							setTimeout(() => {
								uni.navigateTo({
									url: '../bindInfo/bindInfo',
								});
							}, 1000);
						} else {
							this.$msg.showToast(res.data.msg)
						}
					}).catch(res => {

					})
				} else if (this.userInfo.identity_status == 1) {
					// 微信小程序（收徒挣钱）
					this.loginByWechat()
				} else {
					this.$msg.showToast("请到微信上打开平台")
					// uni.navigateTo({
					// 	url: '../promotedMaster/discipleList'
					// })
          // if(this.isWechat()){
          //   apiWechatLogin({}).then(res =>{
          //     if(res.data.code == 1)  {
          //       let appid = res.data.msg
          //       let link = this.GLOBAL.MAIN_URL + 'pages/wxAuth/wxAuth?masterCode=1'
          //       let uri = encodeURIComponent(link);
          //       let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
          //       window.location.href = authURL
          //     }
          //   }).catch(res => {
          //     this.$msg.showToast(res.data.msg)
          //   })
          // } else {
          //   this.$msg.showToast('请到微信上打开平台')
          // }
				}
			},
			// 判断是否是微信平台
			isWechat() {
      			return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
			},
			// 调起微信登录
			loginByWechat() {
      			if(this.isWechat()) {
					apiWechatLogin({}).then(res => {
						if(res.data.code == 1) {
							let appid = res.data.msg
							let link = this.GLOBAL.MAIN_URL + 'pages/promotedMaster/discipleList'
							let uri = encodeURIComponent(link);
							let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
							window.location.href = authURL
						}
					}).catch(res => {
						this.$msg.showToast(res.data.msg)
					})
				} else {
					this.$msg.showToast("请到微信上打开平台")
				}
			},
			promotionMaster(){
				// if (!this.upgradeCode) {
				// 	this.$msg.showToast('升级码不能为空')
				// 	return
				// }
				uni.showLoading({
					title: '提交中',
					mask: true
				})
				apiApplyMaster({
					token: this.token
					// upgradeCode: this.upgradeCode
				}).then(res =>{
				    uni.hideLoading()
				    if (res.data.code == 1) {
				    	// this.showModal = false
						this.masterModal = false
				    	this.upgradeCode = ''
						this.$msg.showToast('申请成功，等待审核')
				    	this.getUserInfo()
				    } else{
				    	this.$msg.showToast(res.data.msg)
				    }
				}).catch(res => {
					this.$msg.showToast(res.data.msg)
				})
			},
			toAccountPage(){
				uni.navigateTo({
					url: '../bindAccount/bindAccount'
				})
			},
      toWithdrawalPage(){
        uni.navigateTo({
          url: '../money/withdrawRecord'
        })
      },
      toCashPage(){
        uni.navigateTo({
          url: '../money/cash'
        })
      },
			toBindInfoPage (){
				uni.navigateTo({
					url: '../bindInfo/bindInfo'
				})
			},
			toServicePage (){
				uni.navigateTo({
					url:'../service/service'
				})
			},
			toSetPage(){
				uni.navigateTo({
					url:'../set/set'
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F5F5;
	}
	#user-page {
		width: 100%;
		overflow: hidden;
		.copy-btn {
			display: inline-block;
			background: $theme-color;
			padding: 0px 20upx;
			border-radius: 10upx;
		    margin-left: 10upx;
			color: #fff;
			font-size: 26upx;
		}
		.page-header {
			width: 100%;
			height: 300upx;
			background: $theme-background-color;
			position: relative;
			.user-info {
				display: flex;
				align-items: center;
				position: relative;
				z-index: 5;
				padding-top: 70upx;
				.header-img {
					margin: 50upx 12upx 0upx 32upx;
					width: 104upx;
					height: 104upx;
					border-radius: 50%;
					background: #FFFFFF;
				}
				.phone-name {
					margin-top: 50upx;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					height: 104upx;
					box-sizing: border-box;
					.name {
						font-size: 34upx;
						font-family: PingFangSC-Semibold, PingFang SC;
						font-weight: 600;
						color: #FFFFFF;
						line-height: 40upx;
					}
					.phone {
						font-size: 28upx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: #FFFFFF;
						line-height: 28upx;
					}
				}
			}
			.task-num-box {
				height: 152upx;
				background: #FFFFFF;
				box-shadow: 0px 0px 10upx 0px rgba(0, 0, 0, 0.1);
				border-radius: 20upx;
				display: flex;
				position: absolute;
				z-index: 10;
				left: 32upx;
				right: 32upx;
				bottom: -110upx;
				.task-type-box {
					flex: 1;
					text-align: center;
					.number {
						font-size: 36upx;
						font-family: DINAlternate-Bold, DINAlternate;
						font-weight: bold;
						color: #333333;
						line-height: 36upx;
						margin-top: 32upx;
						margin-bottom: 28upx;
					}
					.title{
						font-size: 24upx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
						line-height: 24upx;
					}
				}
			}
		}
		.page-header:after {
		    background: $theme-background-color;
		    content: '';
		    width: 160%;
		    height: 400upx;
		    position: absolute;
		    left: -30%;
		    top:0;
		    border-radius: 0 0 50% 50%;
		}
		.row-list {
			background: #FFFFFF;
			box-shadow: 0px 0px 10upx 0px rgba(0, 0, 0, 0.1);
			border-radius: 20upx;
			margin: 144upx 32upx 0 32upx;
			.row-cell-right {
				display: inline-block;
				position: absolute;
				right: 20upx;
				color: #666666;
				font-size: 30upx;
				.right-content {
					display: inline-block;
				}
				.row-right-icon {
					vertical-align: top;
					margin-left: 10upx;
				}
			}
			.row-cell {
				position: relative;
				border-bottom: 1px solid #F8F8F8;
				padding: 0 24upx 0 38upx;
				height: 106upx;
				line-height: 106upx;
				.row-cell-icon {
					display: inline-block;
					font-size: 40upx;
					margin-right: 20upx;
					line-height: 106upx;
					&.account-color {
						color: #FF5001;
					}
					&.info-color{
						color: #FFC901;
					}
					&.disciple-color {
						color: #FF7E4D;
					}
					&.service-color {
						color: #FF4D4D;
					}
					&.set-color {
						color: #488CF0;
					}
				}
				.row-cell-title {
					display: inline-block;
					font-size: $font-base;
					vertical-align: top;
					color: #333333;
					font-size: 30upx;
				}
				.row-cell-right-icon {
					display: inline-block;
					position: absolute;
					right: 20upx;
				}
			}
		}
    .row-list-ad {
      width: 100%;
      border-radius: 20upx;
      margin: 25upx 32upx 0 32upx;
      .row-cell-right {
        display: inline-block;
        position: absolute;
        right: 20upx;
        color: #666666;
        font-size: 30upx;
        .right-content {
          display: inline-block;
        }
        .row-right-icon {
          vertical-align: top;
          margin-left: 10upx;
        }
      }
      .row-cell {
        position: relative;
        border-bottom: 1px solid #F8F8F8;
        padding: 0 24upx 0 38upx;
        height: 106upx;
        line-height: 106upx;
        .row-cell-icon {
          display: inline-block;
          font-size: 40upx;
          margin-right: 20upx;
          line-height: 106upx;
          &.account-color {
            color: #FF5001;
          }
          &.info-color{
            color: #FFC901;
          }
          &.disciple-color {
            color: #FF7E4D;
          }
          &.service-color {
            color: #FF4D4D;
          }
          &.set-color {
            color: #488CF0;
          }
        }
        .row-cell-title {
          display: inline-block;
          font-size: $font-base;
          vertical-align: top;
          color: #333333;
          font-size: 30upx;
        }
        .row-cell-right-icon {
          display: inline-block;
          position: absolute;
          right: 20upx;
        }
      }
    }
		.upgrade-code-input {
			border: 1px solid #e0e0e0;
			font-size: $font-base;
			padding: 16upx 20upx;
			border-radius: 10upx;
			margin-top: 10upx;
		}
	}
	#notice-detail {
		background: white;
		overflow: auto;
		min-height: 45vh;
		max-height: 70vh;
		.titie {
			font-weight: 600;
			padding: 40upx 32upx;
			font-size: 36upx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 30px;
			color: #333;
			opacity: 1;
		}

		.time {
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 32upx;
			color: #999;
			opacity: 1;
			padding: 0upx 32upx 30upx 32upx;

		}

		.content {
			padding: 0px 32upx;
		}
	}

	#urge-detail {
		height: 150upx;
		line-height: 150upx;
		text-align: center;
		font-size: 36upx;
		text {
			color: red;
		}
	}
</style>
